raw_data <- read_csv("./data/raw_experiment_data.csv")

main_df <-
  raw_data %>%
  mutate(pub_official_finish = case_when(player.id_in_group == group.public_official & !is.na(player.phone_number) ~ 1,
                                         TRUE ~ 0),
         player_finish = case_when(!is.na(player.phone_number) ~ 1,
                                   TRUE ~ 0),
         finish_demos = case_when(!is.na(player.ethnicity) & player.ethnicity != 0 ~ 1,
                                  TRUE ~ 0)) %>%
  group_by(group.id_in_subsession, session.code) %>%
  mutate(pub_official_finish = mean(pub_official_finish) > 0,
         all_players_finish = mean(player_finish) == 1,
         all_finish_demos = mean(finish_demos) == 1) %>%
  ungroup() %>%
  mutate(actual_dictator_payoff = case_when(group.public_official == 1 ~ group.payoff1,
                                            group.public_official == 2 ~ group.payoff2,
                                            group.public_official == 3 ~ group.payoff3,
                                            TRUE ~ NA_real_)) %>%
  filter(group.dictator_payoff == actual_dictator_payoff) %>%
  mutate_at(vars(player.vign_1:player.vign_3, player.post1:player.post5), funs(case_when(. == 0 ~ NA_real_,
                                                                                         TRUE ~ .))) %>%
  mutate(player.vign_1 = 6 - player.vign_1) %>%
  select(session.code, participant.code, group.id_in_subsession, player.id_in_group, group.treatment, group.public_official, player.phone_number, player.age:player.ethnicity,
         group.dictator_payoff, group.payoff1, group.payoff2, group.payoff3,
         player.vign_1:player.vign_3, player.post1:player.post6_dictator, pub_official_finish, all_players_finish, all_finish_demos)



began_experiment <-
  main_df %>%
  mutate(cluster = paste0(session.code, group.id_in_subsession)) %>%
  filter(all_finish_demos == T)

finished_experiment <-
  main_df %>%
  mutate(cluster = paste0(session.code, group.id_in_subsession)) %>%
  filter(all_players_finish == T)



finished_experiment %<>% 
  mutate(all_vign = case_when(player.vign_1 %in% c(3,4,5) & player.vign_2 %in% c(3,4,5) & player.vign_3 %in% c(3, 4,5) ~ 1,
                              TRUE ~ 0),
         any_vign = case_when(player.vign_1%in% c(3,4,5) | player.vign_2 %in% c(3,4,5) | player.vign_3 %in% c(3,4,5) ~ 1,
                              TRUE ~ 0)) %>%
  rowwise() %>% 
  mutate(avg_vign = sum(c_across(cols = c(player.vign_1, player.vign_2, player.vign_3)), na.rm = T)) %>%
  ungroup() %>%
  mutate(avg_vign = avg_vign/3) %>%
  mutate(non_pa_payout1 = case_when(group.public_official == 1 ~ group.payoff2,
                                    group.public_official == 2 ~ group.payoff1,
                                    group.public_official == 3 ~ group.payoff1,
                                    TRUE ~ NA_real_)) %>%
  mutate(non_pa_payout2 = case_when(group.public_official == 1 ~ group.payoff3,
                                    group.public_official == 2 ~ group.payoff3,
                                    group.public_official == 3 ~ group.payoff2,
                                    TRUE ~ NA_real_)) %>%
  mutate_at(vars(player.post1:player.post5), funs(case_when(. == 6 ~ NA_real_,
                                                            TRUE ~ .))) %>%
  mutate(perc_other = (non_pa_payout1 + non_pa_payout2)/200000,
         total_payoff = group.dictator_payoff + non_pa_payout1 + non_pa_payout2,
         herf_index_payout = (group.dictator_payoff/total_payoff)^2 + (non_pa_payout1/total_payoff)^2 +  (non_pa_payout2/total_payoff)^2) %>%
  mutate(group.treatment = factor(group.treatment, levels = c("patronage", "random", "merit"))) %>%
  mutate(pub_official_indicator = case_when(player.id_in_group == group.public_official ~ 1,
                                            TRUE ~ 0)) %>%
  mutate(bos_indicator = case_when(group.treatment == "patronage" & player.id_in_group == 1 ~ 1,
                                   group.treatment == "patronage" & player.id_in_group != 1 ~ 0,
                                   TRUE ~ NA_real_))

began_experiment %<>%
  mutate(all_vign = case_when(player.vign_1 %in% c(3,4,5) & player.vign_2 %in% c(3,4,5) & player.vign_3 %in% c(3, 4,5) ~ 1,
                              TRUE ~ 0),
         any_vign = case_when(player.vign_1%in% c(3,4,5) | player.vign_2 %in% c(3,4,5) | player.vign_3 %in% c(3,4,5) ~ 1,
                              TRUE ~ 0)) %>%
  rowwise() %>% 
  mutate(avg_vign = sum(c_across(cols = c(player.vign_1, player.vign_2, player.vign_3)), na.rm = T)) %>%
  ungroup() %>%
  mutate(avg_vign = avg_vign/3) %>%
  mutate(non_pa_payout1 = case_when(group.public_official == 1 ~ group.payoff2,
                                    group.public_official == 2 ~ group.payoff1,
                                    group.public_official == 3 ~ group.payoff1,
                                    TRUE ~ NA_real_)) %>%
  mutate(non_pa_payout2 = case_when(group.public_official == 1 ~ group.payoff3,
                                    group.public_official == 2 ~ group.payoff3,
                                    group.public_official == 3 ~ group.payoff2,
                                    TRUE ~ NA_real_)) %>%
  mutate_at(vars(player.post1:player.post5), funs(case_when(. == 6 ~ NA_real_,
                                                            TRUE ~ .))) %>%
  mutate(perc_other = (non_pa_payout1 + non_pa_payout2)/200000,
         total_payoff = group.dictator_payoff + non_pa_payout1 + non_pa_payout2,
         herf_index_payout = (group.dictator_payoff/total_payoff)^2 + (non_pa_payout1/total_payoff)^2 +  (non_pa_payout2/total_payoff)^2) %>%
  mutate(group.treatment = factor(group.treatment, levels = c("patronage", "random", "merit"))) %>%
  mutate(pub_official_indicator = case_when(player.id_in_group == group.public_official ~ 1,
                                            TRUE ~ 0))
#make IPW data

# Logit model to predict finishing
model_predict <- glm(all_players_finish == T ~ player.age + factor(player.education == 4) + factor(player.gender == 1) +
                       factor(player.religion == 1) + factor(player.ethnicity == 1),
                     family = binomial(link = "logit"),
                     data = began_experiment)  


began_experiment <- 
  augment_columns(model_predict, began_experiment,
                  type.predict = "response") %>% 
  rename(propensity = .fitted) %>% 
  mutate(ipw = (all_players_finish / propensity) + ((1 - all_players_finish) / (1 - propensity)))

began_experiment_ipw <- 
  began_experiment %>% 
  filter(all_players_finish == T, player.id_in_group == group.public_official) %>%
  mutate(group.treatment = factor(group.treatment, levels = c("random", "merit", "patronage")))



finished_experiment %<>%
  mutate(island = case_when(player.province %in% c(11, 12, 13, 14, 15, 16) ~ 1,
                            player.province %in% c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) ~ 2,
                            TRUE ~ 3),
         gender = case_when(player.gender == 1 ~ 1,
                            TRUE ~ 2),
         religion = case_when(player.religion == 1 ~ 1,
                              player.religion %in% c(2, 3) ~ 2,
                              TRUE ~ 3),
         age_cat = case_when(player.age <= 24 ~ 1,
                             player.age > 24 & player.age <= 26 ~ 2,
                             player.age > 26 & player.age <= 29 ~ 3,
                             player.age > 29 ~ 4))



#make weights

data.svy.unweighted <- svydesign(ids=~1, data=finished_experiment)

gender.dist <- data.frame(gender=c(1,2), Freq=nrow(finished_experiment)*c(0.407,.593))

island.dist  <- data.frame(island=c(1,2,3), Freq=nrow(finished_experiment)*c(.422,.298,.28))

relg.dist  <- data.frame(religion=c(1,2,3), Freq=nrow(finished_experiment)*c(.864, 0.119,0.017))

age.dist  <- data.frame(age_cat=c(1,2,3,4), Freq=nrow(finished_experiment)*c(0.25, 0.25,0.25, 0.25))

data.svy.rake <- rake(design = data.svy.unweighted,
                      sample.margins <- list(~gender,~island, ~religion),
                      population.margins <- list(gender.dist, island.dist, relg.dist))


data.svy.rake.trim <- trimWeights(data.svy.rake, lower=.3, upper=2, strict=TRUE)

# This puts the weights in a WEIGHTS variable:

finished_experiment$weights <- weights(data.svy.rake)
finished_experiment$trim_weights <- weights(data.svy.rake.trim)



#keep the data for the replication analyses

main_df <-
  finished_experiment %>%
  select(-c(participant.code, group.id_in_subsession, player.id_in_group, group.public_official, player.phone_number,
            pub_official_finish,all_players_finish,  all_finish_demos)) 


supp_df <-
  began_experiment %>%
  select(-c(participant.code, group.id_in_subsession, player.id_in_group, group.public_official, player.phone_number,
            pub_official_finish, all_finish_demos))

